package com.situ.controller;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.situ.bean.Product;
import com.situ.bean.User;
import com.situ.service.ICartService;
import com.situ.service.impl.ProductService;
import com.situ.util.LayData;
import com.situ.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api")
public class GenerateJsonFiles {
    @Autowired
    private ProductService productService;
    @Autowired
    private ICartService cartService;
    @GetMapping("/generate-json")
    public ResponseEntity<?> generateJsonFiles(HttpSession session) {



            try {
                if (session.getAttribute(SYS.UPLOAD_USER) != null) {
                    User user = (User) session.getAttribute(SYS.UPLOAD_USER);
                    System.out.println("登录的" + user);
                    List<Product> userProducts = new ArrayList<>(); // 假设这是用户相关产品
                    List<Product> allProducts = productService.getAllofProducts();

                    for (int i = 0; i < cartService.getcart(user).size(); i++) {
                        System.out.println("查到的" + productService.getProductById(cartService.getcart(user).get(i).getProductid()));
                        userProducts.add(productService.getProductById(cartService.getcart(user).get(i).getProductid()));
                    }
                    ObjectMapper mapper = new ObjectMapper();
                    mapper.enable(SerializationFeature.INDENT_OUTPUT);

                    mapper.writeValue(new File("user_products.json"), userProducts);
                    mapper.writeValue(new File("all_products.json"), allProducts);
                    List<Product> recommendedProducts;
                    recommendedProducts = callPythonRecommendation();
                    System.out.println(recommendedProducts);
                    System.out.println("俩个文件已经生成");
                    return ResponseEntity.ok(recommendedProducts);
                }else{
                    productService.getAllProducts();
                    List<Product> allProducts =new ArrayList<>();
                    for(int i = 0;i<8;i++){
                        allProducts.add(productService.getAllProducts().get(i));
                    }
                    return ResponseEntity.ok(allProducts);
                }
              } catch (Exception e) {
                productService.getAllProducts();
                List<Product> allProducts =new ArrayList<>();
                for(int i = 0;i<8;i++){
                    allProducts.add(productService.getAllProducts().get(i));
                }
                return ResponseEntity.ok(allProducts);

            }


    }





    private List<Product> callPythonRecommendation() throws IOException, InterruptedException {
        // 1. 直接在项目目录下执行Python脚本
        Process process = Runtime.getRuntime().exec("cmd /c cd C:\\Users\\Administrator\\IdeaProjects\\Gshop && python recommend.py");

        // 2. 捕获输出（调试用）
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println("[Python输出] " + line);
        }


        // 4. 读取结果
        return new ObjectMapper().readValue(
                new File("recommendedproducts.json"),
                new TypeReference<List<Product>>(){}
        );
    }

}















